const exp = require('express'),
    fs = require('fs'),
    bodyParser = require('body-parser'),
    login = exp.Router();
login.get('/api/login/user', (req, res) => {
    fs.exists(`user/${req.query.user}.json`, (exists) => {
        if (exists) {
            var data = JSON.parse(fs.readFileSync(`user/${req.query.user}.json`).toString());
            if (data.psw == req.query.psw) {
                res.cookie('petname',req.query.user);
                res.status(200).json({
                    code:'success',
                    message:'登录成功'
                })
            }else{
                res.status(200).json({
                    code:'error',
                    message:'密码错误'
                })
            }
        }else{
            res.status(200).json({
                code:'error',
                message:'用户名不存在'
            })
        }
    })
})
login.get('/api/user/signout', (req, res) => {
    // 清除cookie
    res.clearCookie('petname');
    res.status(200).json({ code: 'success' ,message:'退出成功'})
})
module.exports = login;